//
//  SAAnnotaionView.m
//  SuperApp
//
//  Created by VanJay on 2020/4/23.
//  Copyright © 2020 chaos network technology. All rights reserved.
//

#import "SAAnnotationView.h"
#import "SAAnnotation.h"


@interface SAAnnotationView ()
/// 图片
@property (nonatomic, strong) UIImageView *logoIV;
/// 提示
@property (nonatomic, strong) BaseLabel *tipLabel;
@end

@implementation SAAnnotationView

- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addSubview:self.logoIV];
        [self addSubview:self.tipLabel];
    }
    return self;
}

- (void)updateConstraints {
    [self.logoIV mas_remakeConstraints:^(MASConstraintMaker *make) {
        if (!self.logoIV.isHidden) {
            make.width.equalTo(self).offset(-2 * 5);
            make.height.equalTo(self.logoIV.mas_width);
            make.centerX.equalTo(self);
            make.top.equalTo(self).offset(5);
        }
    }];

    [self.tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        if (!self.tipLabel.isHidden) {
            make.left.equalTo(self.mas_right).offset(-10);
            make.centerY.equalTo(self.mas_top);
        }
    }];

    [super updateConstraints];
}

#pragma mark - setter
- (void)setAnnotation:(id<MKAnnotation>)annotation {
    [super setAnnotation:annotation];

    SAAnnotation *ann = (SAAnnotation *)annotation;

    self.logoIV.hidden = ann.logoImageURL && ann.logoImage;
    if (!self.logoIV.isHidden) {
        if (ann.logoImageURL) {
            [self.logoIV sd_setImageWithURL:[NSURL URLWithString:ann.logoImageURL]];
        } else {
            self.logoIV.image = ann.logoImage;
        }
    }

    self.tipLabel.hidden = ann.tipTitle;
    if (!self.tipLabel.isHidden) {
        self.tipLabel.text = ann.tipTitle;
    }

    [self setNeedsUpdateConstraints];
}

#pragma mark - lazy load
- (UIImageView *)logoIV {
    if (!_logoIV) {
        UIImageView *imageView = UIImageView.new;
        imageView.hidden = true;
        _logoIV = imageView;
    }
    return _logoIV;
}

- (BaseLabel *)tipLabel {
    if (!_tipLabel) {
        BaseLabel *label = BaseLabel.new;
        label.font = wFontSize(10);
        label.textColor = wColor_3;
        label.numberOfLines = 1;
        label.backgroundColor = UIColor.whiteColor;
        label.hd_edgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        _tipLabel = label;
    }
    return _tipLabel;
}
@end
